﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{

    public class PhanCongService
    {
        public List<PhanCong> GetPhanCongList(int maLopHoc)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<PhanCong> list = context.PhanCongs.Where(m => m.MaLopHoc == maLopHoc).ToList();
                    //List<PhanCong> list = context.PhanCongs.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::GetPhanCongList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static PhanCongViewModel GetPhanCongList(int pageNum, int pageIndex, int maLopHoc)
        {
            try
            {
                if (maLopHoc != -1)
                {
                    using (LMSDataEntities context = new LMSDataEntities())
                    {
                        //List<PhanCong> list = context.PhanCongs.Include("Role").OrderBy(m => m.signupDate).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                        //List<PhanCong> list = context.PhanCongs.Include("TaiKhoan").Where(m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_STUDENT).OrderBy(m => m.TaiKhoan.NgayDangKy).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                        List<PhanCong> list = context.PhanCongs.Where(m => m.MaLopHoc == maLopHoc).OrderBy(m => m.NgayDay).Skip(pageNum * (pageIndex - 1)).Take(pageNum).Distinct().ToList();
                        foreach (PhanCong pc in list)
                        {
                            pc.LopHocReference.Load();
                            pc.GiaoVienReference.Load();
                            pc.GiaoVienReference.Value.TaiKhoanReference.Load();
                            //pc.CaHocReference.Load();
                            pc.LopHocReference.Value.ChiNhanhReference.Load();
                        }
                        int total = context.PhanCongs.Where(m => m.MaLopHoc == maLopHoc).Distinct().Count();
                        return new PhanCongViewModel(list, total);
                    }
                }
                else
                {
                    using (LMSDataEntities context = new LMSDataEntities())
                    {
                        List<PhanCong> list = context.PhanCongs.OrderBy(m => m.NgayDay).Skip(pageNum * (pageIndex - 1)).Take(pageNum).Distinct().ToList();
                        int total = 0;
                        if (list.Count > 0)
                        {
                            int ma = int.Parse(list.ElementAt(0).MaLopHoc.ToString());
                            foreach (PhanCong pc in list)
                            {
                                if(ma != pc.MaLopHoc)
                                pc.LopHocReference.Load();
                                pc.GiaoVienReference.Load();
                                pc.GiaoVienReference.Value.TaiKhoanReference.Load();
                                //pc.CaHocReference.Load();
                            }


                            total = context.PhanCongs.Distinct().Count();
                        }
                        return new PhanCongViewModel(list, total);
                    }
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::GetPhanCongList", ex.StackTrace, ex.Message);
                return new PhanCongViewModel();
            }
        }

        //public static bool InsertPhanCong(PhanCong comp)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            context.AddToPhanCongs(comp);
        //            context.SaveChanges();
        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("PhanCongServices::InsertPhanCong", ex.StackTrace, ex.Message);
        //        return false;
        //    }
        //}

        //public static bool DeletePhanCong(int id)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            PhanCong obj = context.PhanCongs.Where(c => c.ID == id).FirstOrDefault();
        //            if (obj != null)
        //            {
        //                obj.Deleted = Utils.Constant.GENERAL_STATUS_DELETED;
        //                //context.PhanCongs.DeleteObject(obj);
        //                context.SaveChanges();
        //                return true;
        //            }
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("PhanCongServices::DeletePhanCong", ex.StackTrace, ex.Message);
        //        return false;
        //    }
        //}

        //public static PhanCong GetPhanCongById(int id)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            return context.PhanCongs.Where(c => c.ID == id).FirstOrDefault();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("PhanCongServices::GetPhanCongById", ex.StackTrace, ex.Message);
        //        return null;
        //    }
        //}

        public static bool Edit(PhanCong newModel)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    PhanCong model = context.PhanCongs.Where(c => c.ID == newModel.ID).FirstOrDefault();

                    if (model != null)
                    {
                        model.CaHoc = newModel.CaHoc;
                        model.MaCaHoc = newModel.MaCaHoc;
                        model.Thu = newModel.Thu;
                        if (newModel.NgayDay.ToString() != null)
                        {
                            model.NgayDay = Convert.ToDateTime(newModel.NgayDay.ToString());
                        }
                        
                        model.MaGiaoVien = newModel.MaGiaoVien;

                        context.SaveChanges();

                        //cap nhat bang diem danh
                        BangDiemDanh dbd = context.BangDiemDanhs.Where(b => b.MaPhanCong == model.ID).FirstOrDefault();
                        if (dbd != null)
                        {
                            dbd.Ngay = model.NgayDay;
                            context.SaveChanges();
                        }

                        return true;
                    }


                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
        }

        ////authenticate
        //public static PhanCong GetPhanCong(string tenPhanCong)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    try
        //    {
        //        return context.PhanCongs.Single(a => a.TenPhanCong.Equals(tenPhanCong));

        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("PhanCongServices::GetPhanCong", ex.StackTrace, ex.Message);
        //        return null;
        //    }
        //    finally
        //    {
        //        context.Dispose();
        //    }
        //}        

        public static bool InsertPhanCongTKB(int maLopHoc, int maGiaoVien, string maCaHoc, string thu, string ngayBatDau)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                Utils.Converter cv = new Utils.Converter();
                //sau nay so 12 se lay o bang ThamSo -- lam sau
                DateTime ngayHoc = cv.StringToDate(ngayBatDau);

                //insert 12 buoi hoc vao bang phan cong
                LopHoc lop = new LopHoc();
                lop = context.LopHocs.FirstOrDefault(l => l.ID == maLopHoc);
                GiaoVien gv = new GiaoVien();
                gv = context.GiaoViens.FirstOrDefault(l => l.ID == maGiaoVien);
                //CaHoc ca = new CaHoc();
                //ca = context.CaHocs.FirstOrDefault(l => l.ID == maCaHoc);

                for (int i = 1; i <= 12; i++)
                {
                    PhanCong pc = new PhanCong();
                    pc.LopHoc = lop;
                    pc.GiaoVien = gv;
                    //pc.CaHoc = ca;
                    pc.CaHoc = maCaHoc;
                    pc.Thu = thu;
                    pc.NgayDay = ngayHoc;
                    ngayHoc = ngayHoc.AddDays(7);

                    context.AddToPhanCongs(pc);
                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::InsertPhanCong", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertPhanCongTKB(int maLopHoc, int maGiaoVien, int maCaHoc, string thu, string ngayBatDau)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                Utils.Converter cv = new Utils.Converter();
                //sau nay so 12 se lay o bang ThamSo -- lam sau
                DateTime ngayHoc = cv.StringToDate(ngayBatDau);

                //insert 12 buoi hoc vao bang phan cong
                LopHoc lop = new LopHoc();
                lop = context.LopHocs.FirstOrDefault(l => l.ID == maLopHoc);
                GiaoVien gv = new GiaoVien();
                gv = context.GiaoViens.FirstOrDefault(l => l.ID == maGiaoVien);
                //CaHoc ca = new CaHoc();
                //ca = context.CaHocs.FirstOrDefault(l => l.ID == maCaHoc);

                for (int i = 1; i <= 12; i++)
                {
                    PhanCong pc = new PhanCong();
                    pc.LopHoc = lop;
                    pc.GiaoVien = gv;
                    //pc.CaHoc = ca;
                    pc.MaCaHoc = maCaHoc;
                    pc.Thu = thu;
                    pc.NgayDay = ngayHoc;
                    ngayHoc = ngayHoc.AddDays(7);

                    context.AddToPhanCongs(pc);
                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::InsertPhanCong", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        //Added by Kha
        //
        public PhanCongViewModel TatCaPhanCongTheoThang(int id, int m, int y)
        {
            var context = new LMSDataEntities();
            try
            {
                var lpc = (from p in context.PhanCongs
                           from tk in context.TaiKhoans
                           from gv in context.GiaoViens
                           where tk.ID == id && tk.ID == gv.MaTaiKhoan && p.MaGiaoVien == gv.ID && p.NgayDay.Value.Month == m && p.NgayDay.Value.Year == y
                           select new PhanCongMoRongModel
                           {
                               Pc = p,
                               TenGiaoVien = p.GiaoVien.TaiKhoan.HoTen,
                               //TenCaHoc = p.CaHoc.TenCaHoc,
                               TenCaHoc = p.CaHoc,
                               TenLopHoc = p.LopHoc.TenLopHoc,
                               TienCong = (double)(from llh_gv in context.LoaiLopHoc_GiaoVien
                                                   from lh in context.LopHocs
                                                   where m >= llh_gv.NgayBatDau.Value.Month && m <= llh_gv.NgayKetThuc.Value.Month &&
                                                  y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                                   lh.ID == p.MaLopHoc && lh.MaLoaiLopHoc == llh_gv.MaLoaiLopHoc
                                                   select llh_gv.Luong).FirstOrDefault()
                           })
                           .OrderBy(p => p.Pc.NgayDay).ToList();
                var danhsachtiencong = (from tc in lpc select tc.TienCong).ToList();
                int total = danhsachtiencong.Count();
                PhanCongViewModel pcvm = new PhanCongViewModel(lpc, total);
                pcvm.TinhTongTienCong(danhsachtiencong);
                return pcvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::PhanCongTheoThang", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        //
        public static PhanCongViewModel PhanCongTheoThang(int id, int m, int y,int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {            
                var lpc = (from p in context.PhanCongs 
                           from tk in context.TaiKhoans
                           from gv in context.GiaoViens
                           where tk.ID==id && tk.ID == gv.MaTaiKhoan && tk.Deleted!=1 && gv.Deleted!=1 && p.MaGiaoVien == gv.ID  && p.NgayDay.Value.Month == m && p.NgayDay.Value.Year == y 
                           //select new PhanCongMoRongModel {Pc = p, TenGiaoVien = p.GiaoVien.TaiKhoan.HoTen, TenCaHoc = p.CaHoc.TenCaHoc,
                            select new PhanCongMoRongModel {Pc = p, TenGiaoVien = p.GiaoVien.TaiKhoan.HoTen, TenCaHoc = p.CaHoc,

                               TenLopHoc = p.LopHoc.TenLopHoc,
                               TienCong = (double)(from llh_gv in context.LoaiLopHoc_GiaoVien 
                                           from lh in context.LopHocs
                                            where m>=llh_gv.NgayBatDau.Value.Month && m<=llh_gv.NgayKetThuc.Value.Month &&
                                                  y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                            lh.ID==p.MaLopHoc && lh.MaLoaiLopHoc==llh_gv.MaLoaiLopHoc select llh_gv.Luong).FirstOrDefault()})
                           .OrderBy(p => p.Pc.NgayDay).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                var danhsachtiencong = (from p in context.PhanCongs
                             from tk in context.TaiKhoans
                             from gv in context.GiaoViens
                             where tk.ID == id && tk.ID == gv.MaTaiKhoan && tk.Deleted != 1 && gv.Deleted != 1 && p.MaGiaoVien == gv.ID && p.NgayDay.Value.Month == m && p.NgayDay.Value.Year == y
                             select (double)(from llh_gv in context.LoaiLopHoc_GiaoVien
                                                     from lh in context.LopHocs
                                             where m >= llh_gv.NgayBatDau.Value.Month && m <= llh_gv.NgayKetThuc.Value.Month &&
                                                    y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                                     lh.ID == p.MaLopHoc && lh.MaLoaiLopHoc == llh_gv.MaLoaiLopHoc
                                                     select llh_gv.Luong).FirstOrDefault()).ToList();
                int total = danhsachtiencong.Count();
                PhanCongViewModel pcvm = new PhanCongViewModel(lpc, total);
                pcvm.TinhTongTienCong(danhsachtiencong);
                return pcvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::PhanCongTheoThang", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public static PhanCongViewModel PhanCongTheoThangChiNhanh(int id, int m, int y,int cn, int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {
                var lpc = (from p in context.PhanCongs
                           from tk in context.TaiKhoans
                           from gv in context.GiaoViens
                           where p.LopHoc.MaChiNhanh==cn && tk.ID == id && tk.ID == gv.MaTaiKhoan && tk.Deleted != 1 && gv.Deleted != 1 && p.MaGiaoVien == gv.ID && p.NgayDay.Value.Month == m && p.NgayDay.Value.Year == y
                           select new PhanCongMoRongModel
                           {
                               Pc = p,
                               TenGiaoVien = p.GiaoVien.TaiKhoan.HoTen,
                               //TenCaHoc = p.CaHoc.TenCaHoc,
                               TenCaHoc = p.CaHoc,
                               TenLopHoc = p.LopHoc.TenLopHoc,
                               TienCong = (double)(from llh_gv in context.LoaiLopHoc_GiaoVien
                                                   from lh in context.LopHocs
                                                   where m >= llh_gv.NgayBatDau.Value.Month && m <= llh_gv.NgayKetThuc.Value.Month &&
                                                         y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                                   lh.ID == p.MaLopHoc && lh.MaLoaiLopHoc == llh_gv.MaLoaiLopHoc
                                                   select llh_gv.Luong).FirstOrDefault()
                           })
                           .OrderBy(p => p.Pc.NgayDay).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                var danhsachtiencong = (from p in context.PhanCongs
                                        from tk in context.TaiKhoans
                                        from gv in context.GiaoViens
                                        where p.LopHoc.MaChiNhanh == cn && tk.ID == id && tk.ID == gv.MaTaiKhoan && tk.Deleted != 1 && gv.Deleted != 1 && p.MaGiaoVien == gv.ID && p.NgayDay.Value.Month == m && p.NgayDay.Value.Year == y
                                        select (double)(from llh_gv in context.LoaiLopHoc_GiaoVien
                                                        from lh in context.LopHocs
                                                        where m >= llh_gv.NgayBatDau.Value.Month && m <= llh_gv.NgayKetThuc.Value.Month &&
                                                               y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                                                lh.ID == p.MaLopHoc && lh.MaLoaiLopHoc == llh_gv.MaLoaiLopHoc
                                                        select llh_gv.Luong).FirstOrDefault()).ToList();
                int total = danhsachtiencong.Count();
                PhanCongViewModel pcvm = new PhanCongViewModel(lpc, total);
                pcvm.TinhTongTienCong(danhsachtiencong);
                return pcvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::PhanCongTheoThang", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public BaoCaoViewModel BaoCaoDoanhThu(int m, int y, int chinhanh)
        {
            var context = new LMSDataEntities();
            try
            {
                var pclmr = (from pc in context.PhanCongs
                           from lh in context.LopHocs
                           where pc.NgayDay.Value.Month == m && pc.NgayDay.Value.Year == y && lh.MaChiNhanh == chinhanh
                            && pc.MaLopHoc == lh.ID
                           select new PhanCongMoRongModel
                           {
                               Pc = pc,
                               TienCong = (double)(from llh in context.LoaiLopHocs
                                                   from llh_gv in context.LoaiLopHoc_GiaoVien
                                                   where m >= llh_gv.NgayBatDau.Value.Month && m <= llh_gv.NgayKetThuc.Value.Month &&
                                                  y >= llh_gv.NgayBatDau.Value.Year && y <= llh_gv.NgayKetThuc.Value.Year &&
                                                   llh.ID == lh.MaLoaiLopHoc && llh.ID == llh_gv.MaLoaiLopHoc
                                                   select llh_gv.Luong).FirstOrDefault()
                           }).ToList();

                var hv_lmr = (from l in context.LopHocs
                              from hv_l in context.HocVien_Lop
                              where l.NgayKhaiGiang.Value.Month == m && l.NgayKhaiGiang.Value.Year == y && l.MaChiNhanh == chinhanh
                              && l.ID == hv_l.MaLopHoc && hv_l.DongHocPhi == 1
                              select new HocVien_LopMoRongModel { HV_L=hv_l }).ToList();
                int totalgv = (from pc in pclmr group pc by pc.Pc.MaGiaoVien into pcgr select pcgr.Count()).FirstOrDefault();
                int totallh = (from hvl in hv_lmr group hvl by hvl.HV_L.MaLopHoc into hvlgr select hvlgr.Count()).FirstOrDefault();
                ChiNhanh cn = (from c in context.ChiNhanhs where c.ID == chinhanh select c).FirstOrDefault();
                BaoCaoViewModel bcvm = new BaoCaoViewModel(pclmr, hv_lmr);
                bcvm.SoLuongGiaoVien = totalgv;
                bcvm.SoLuongLopHoc = totallh;
                bcvm.CN = cn;
                bcvm.TinhTongThu(); bcvm.TinhTongChi(); bcvm.TinhChiThem(bcvm.CN.ID, m, y); bcvm.TinhThuThem(bcvm.CN.ID, m, y); bcvm.TinhLoiNhuan();

                return bcvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::BaoCaoDoanhThu", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public BaoCaoTatCaViewModel BaoCaoDoanhThuTong(int m, int y)
        {
            var context = new LMSDataEntities();
            try
            {
                BaoCaoTatCaViewModel bctcvm = new BaoCaoTatCaViewModel();
                var cnl = (from c in context.ChiNhanhs select c).ToList();
                foreach (ChiNhanh cn in cnl)
                {
                    var pclmr = (from pc in context.PhanCongs
                                 from lh in context.LopHocs
                                 where pc.NgayDay.Value.Month == m && pc.NgayDay.Value.Year == y && lh.MaChiNhanh == cn.ID
                                  && pc.MaLopHoc == lh.ID
                                 select new PhanCongMoRongModel
                                 {
                                     Pc = pc,
                                     TienCong = (double)(from llh in context.LoaiLopHocs
                                                         from llh_gv in context.LoaiLopHoc_GiaoVien
                                                         where llh.ID == lh.MaLoaiLopHoc && llh.ID == llh_gv.MaLoaiLopHoc
                                                         select llh_gv.Luong).FirstOrDefault()
                                 }).ToList();

                    var hv_lmr = (from l in context.LopHocs
                                  from hv_l in context.HocVien_Lop
                                  where l.NgayKhaiGiang.Value.Month == m && l.NgayKhaiGiang.Value.Year == y && l.MaChiNhanh == cn.ID
                                  && l.ID == hv_l.MaLopHoc && hv_l.DongHocPhi == 1
                                  select new HocVien_LopMoRongModel { HV_L = hv_l }).ToList();
                    BaoCaoViewModel bcvm = new BaoCaoViewModel(pclmr, hv_lmr);
                    bcvm.CN = cn;
                    bcvm.TinhTongThu(); bcvm.TinhTongChi(); bcvm.TinhChiThem(bcvm.CN.ID, m, y); bcvm.TinhThuThem(bcvm.CN.ID, m, y); bcvm.TinhLoiNhuan();
                    bctcvm.ThemBaoCao(bcvm);
                }
                return bctcvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::BaoCaoDoanhThu", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        //End addition

        /// <summary>
        /// Lay danh sach phan cong theo TKB
        /// | Author: NHDuc
        /// | Created Date: 18/12/2013
        /// </summary>
        /// <param name="maLopHoc"></param>
        /// <returns></returns>
        public static List<CalendarModel> GetPhanCongTKB()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<CalendarModel> list = context.PhanCongs.Join(context.LopHocs, p => p.MaLopHoc, l => l.ID,
                            (p, l) => new {PhanCong = p, LopHoc = l})
                            .Join(context.GiaoViens, p => p.PhanCong.MaGiaoVien, g => g.ID,
                            (p, g) => new {PhanCong = p.PhanCong, LopHoc = p.LopHoc, GiaoVien = g})
                            //.Join(context.CaHocs, p => p.PhanCong.MaCaHoc, c => c.ID,
                            //(p, c) => new { PhanCong = p.PhanCong, LopHoc = p.LopHoc, GiaoVien = p.GiaoVien, CaHoc = c })

                            .Join(context.TaiKhoans, g => g.GiaoVien.MaTaiKhoan, t => t.ID,
                            (p, t) => new { PhanCong = p.PhanCong, LopHoc = p.LopHoc, GiaoVien = p.GiaoVien, TaiKhoan = t })
                            .Where(m => m.LopHoc.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL
                                && m.LopHoc.TinhTrang == "Đang Học")
                            .ToList().Select(s => new CalendarModel
                            {
                                id = s.PhanCong.ID,
                                title = s.LopHoc.TenLopHoc,
                                malop = s.LopHoc.ID,
                                start = s.PhanCong.NgayDay.Value.ToString("yyyy/MM/dd"),
                                //start = "2014/05/27",
                                //end = s.CaHoc.TenCaHoc,
                                //thoigianBD = s.CaHoc.ThoiGianBatDau,
                                //thoigianKT = s.CaHoc.ThoiGianKetThuc,
                                //end = s.PhanCong.CaHoc,
                                end = s.PhanCong.NgayDay.Value.ToString("yyyy/MM/dd"),
                                thoigianBD = SplitCaHocDB(s.PhanCong.CaHoc),
                                thoigianKT = SplitCaHocKT(s.PhanCong.CaHoc),
                                giaovien = s.TaiKhoan.HoTen
                            }).ToList();

                    List<CalendarModel> listReturn = new List<CalendarModel>();

                    if (list.Count() > 0)
                    {
                        foreach (CalendarModel c in list)
                        {
                            var dsHocVien = context.HocVien_Lop.Join(context.LopHocs, h => h.MaLopHoc, l => l.ID,
                                (h, l) => new { HocVien_Lop = h, LopHoc = l })
                                .Join(context.HocViens, h => h.HocVien_Lop.MaHocVien, hv => hv.ID,
                                (h, hv) => new { HocVien_Lop = h, LopHoc = h.LopHoc, HocVien = hv })
                                .Join(context.TaiKhoans, h => h.HocVien.MaTaiKhoan, t => t.ID,
                                (h, t) => new { HocVien_Lop = h, LopHoc = h.LopHoc, HocVien = h.HocVien, TaiKhoan = t })
                                .Where(l => l.LopHoc.ID == c.malop)
                                .ToList();
                            if (dsHocVien.Count > 0)
                            {
                                string temp = ""; 
                                foreach (var hocvien in dsHocVien)
                                {
                                    if (!hocvien.TaiKhoan.HoTen.Equals(""))
                                        temp += "- " + hocvien.TaiKhoan.HoTen + "<br/>";
                                }
                                if (!temp.Equals(""))
                                    c.dshocvien = temp;
                                else
                                    c.dshocvien = "";
                            }
                            else
                                c.dshocvien = "(chưa có)";
                        }
                    }

                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongServices::GetPhanCongList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static string SplitCaHocDB(string caHoc)
        {
            if (caHoc.IndexOf("-") != -1)
            {
                string[] arr = caHoc.Split('-');
                return convertDate(arr[0].Trim());
            }
            return DateTime.Now.ToString("yyyy/MM/dd");
        }

        public static string SplitCaHocKT(string caHoc)
        {
            if (caHoc.IndexOf("-") != -1)
            {
                string[] arr = caHoc.Split('-');
                return convertDate(arr[1].Trim());
            }
            return DateTime.Now.ToString("yyyy/MM/dd");
        }

        public static string convertDate(string date)
        {
            if (date.IndexOf("-") != -1)
            {
                string[] arr = date.Split('-');
                return arr[2] + "/" + arr[1] + "/" + arr[0];
            }
            return DateTime.Now.ToString("yyyy/MM/dd");
        }

    }

}
